//
//  ControlTemplateViewCheckController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewCheckController.h"

#pragma mark -
#pragma mark 多选框控件【checkbox】
@interface ControlTemplateViewCheckController () {
    UILabel* valueLabel;
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;
@property (nonatomic, strong) NSArray *selectDataArray;

@end

@implementation ControlTemplateViewCheckController

static ControlTemplateViewCheckController* sharedManagerCheckInter = nil;

+ (ControlTemplateViewCheckController*)sharedManagerCheck
{
    sharedManagerCheckInter = [[self alloc] init];
    
    return sharedManagerCheckInter;
}

- (void)initVar
{
    _valueStr = @"";
    _cnValueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVar];
//    self.isNeedAdpterHotspot = YES;
    
    self.view.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.3];
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    [_bgMainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(TEMPLATE_HEIGHT);
    }];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    NSArray * initArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
    NSArray * attrValuesArr = [SEGUtils arrayWithJsonString:[_params valueForKey:@"attrValue"]];
    NSMutableArray * defaultValueArr = [[NSMutableArray alloc] init];
    NSString * defaultValueString = @"";
    int i =0 ;
    for (NSDictionary * dic in initArray) {
        NSString * _idStr = dic[@"id"];
        if (attrValuesArr.count >0) {
            if ([attrValuesArr containsObject:_idStr]) {
                [defaultValueArr addObject:dic];
                defaultValueString = [defaultValueString length]==0?dic[@"value"]:[NSString stringWithFormat:@"%@,%@", defaultValueString, dic[@"value"]];
            }
        }else{
            if ([dic[@"isDefault"] intValue] == 1) {
                [defaultValueArr addObject:dic];
                defaultValueString = [defaultValueString length]==0?dic[@"value"]:[NSString stringWithFormat:@"%@,%@", defaultValueString, dic[@"value"]];
            }
        }
        i++;
    }
    
    ////////是否必填 //是否可读 0-可不写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        NSDictionary * dataDic;
        if (initArray.count>0) {
            dataDic = initArray.firstObject;
        }
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [valueLabel extendAttributedString:initArray.count>0?[dataDic valueForKey:@"value"]:@"" lineSpace:LINESPACE_6 font:kFont_16];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_1];
        valueLabel.numberOfLines = 0;
        [_bgMainView addSubview:valueLabel];
    }
    else {
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), TEMPLATE_HEIGHT)];
        abtn.tempData = _params;
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        [abtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgMainView);
        }];
        
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgMainView.mas_centerY);
            make.right.equalTo(_bgMainView.mas_right).offset(-HEADSPACE);
        }];
        
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - 2 * CONTENTMARGIN_RIGHT - image.size.width, TEMPLATE_HEIGHT)];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        valueLabel.tag = 999;
        valueLabel.numberOfLines = 0;
        [valueLabel setText:[_params valueForKey:@"markedWords"]];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_C];
        [_bgMainView addSubview:valueLabel];
        
        //判断是否有默认值
        if (initArray.count > 0) {
            [valueLabel extendAttributedString:defaultValueString lineSpace:LINESPACE_6 font:kFont_16];
            [valueLabel setTextColor:kColor_1];
        }
        
        if (attrValuesArr.count>0) {
            _valueStr = [_params valueForKey:@"attrValue"];
        }
        _optional = @"0";
    }
    [self refreshUI];
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    SEGSpaceViewController* spaceVC = [[SEGSpaceViewController alloc] init];
    spaceVC.fundelegate = self;
    spaceVC.typeStr = @"checkbox";
    spaceVC.defaultStr = _valueStr;
    spaceVC.tempDict = _params;
    spaceVC.title = [_params valueForKey:@"title"];
    spaceVC.selectDataArray = [self.selectDataArray copy];
    [self.masterVC.navigationController pushViewController:spaceVC animated:YES];
}

/**
 *  多选框数据
 *
 *  @param data 接收数据字典
 */
- (void)onRequestData:(id)data
{
    self.selectDataArray = [data copy];
    NSArray* paramsArr = data;
    UILabel* alabel = [_bgMainView viewWithTag:999];
    
    if ([paramsArr count] == 0) {
        [alabel setText:[_params valueForKey:@"markedWords"]];
        [alabel setTextColor:kColor_1];
    }
    
    NSString* userStr = nil;
    NSString* userName = nil;
    NSMutableArray * users = [[NSMutableArray alloc] init];
    for (NSDictionary* dict in paramsArr) {
        NSString * idstr = dict[@"id"];
        NSString * valueStr = dict[@"value"];
        NSString * oneuserID = idstr;
        
        if (userStr == nil) {
            userStr = idstr;
            userName = valueStr;
        }
        else {
            userStr = [NSString stringWithFormat:@"%@,%@", userStr, idstr];
            userName = [NSString stringWithFormat:@"%@,%@", userName, valueStr];
        }
        
        [users addObject:oneuserID];
    }
    
    _valueStr = [SEGUtils ValueToString:users];
    _cnValueStr = userName;
    _optional = @"0";
    
    //显示选择值
    [alabel setTextColor:kColor_1];
    [alabel extendAttributedString:userName lineSpace:LINESPACE_6 font:kFont_16];
    [self refreshUI];
}

/**
 *  给多选框控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingCheckBoxView:(NSString*)valueStr
{
    NSArray * initData = [SEGUtils arrayWithJsonString:_params[@"initData"]];
    NSArray* tempArray = [SEGUtils arrayWithJsonString:valueStr];
    _valueStr = valueStr;
    
    NSString * values = @"";
    for (NSDictionary * dic in initData) {
        if ([tempArray containsObject:dic[@"id"]]) {
            values = [values length]==0?dic[@"value"]:[NSString stringWithFormat:@"%@,%@", values, dic[@"value"]];
        }
    }
    _cnValueStr = values;
    
    if ([_valueStr length] > 0) {
        [valueLabel extendAttributedString:_cnValueStr lineSpace:LINESPACE_6 font:kFont_16];
        [valueLabel setTextColor:kColor_1];
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
    [self refreshUI];
    
}

-(void)refreshUI {
    CGSize size = [_cnValueStr getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:CGRectGetWidth(valueLabel.bounds)];
    size.height = size.height > 24 ? size.height : 24;
    size.height = ceil(titleSize.height > size.height ? titleSize.height : size.height);
    valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(valueLabel.bounds), TEMPLATE_HEIGHT -24 + size.height);
    _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(valueLabel.bounds));
    
    [_bgMainView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(TEMPLATE_HEIGHT -24 + size.height);
    }];
    
    
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    if (self.fundelegate) {
        [self.fundelegate onFeeRefeshUI];
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    // 适配开启热点页面下移的问题
    CGRect tempRect = self.view.frame;
    self.view.frame = CGRectMake(tempRect.origin.x, 0, tempRect.size.width, tempRect.size.height);
}

@end
